<?php
/**
 * 海外仓-中转库存调整
 * (model)
 */
namespace App\Models\OverSeasBa;

use App\Models\Basics\SysPlatAccount;
use App\Models\Product\ProductSku;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class BoxAdjust extends Model
{
    protected $table = 'os_b_box_adjust';

    protected $guarded = [];

    public function warehouse(){
        return $this->belongsTo(Warehouse::class,'warehouse_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static function getDownloadData($re){
        $warehouse = [];
        $account = [];
        $user = [];
        foreach ($re as $row){
            $temp = [];
            $temp['batch_no'] = $row->batch_no;
            $temp['box_no'] = $row->box_no;
            $temp['fnsku'] = $row->fnsku;
            if(!array_key_exists($row->account_id,$account)){
                $account[$row->account_id] = SysPlatAccount::getAccountById($row->account_id);
            }
            $temp['account_id'] = $account[$row->account_id];
            if(!array_key_exists($row->warehouse_id,$warehouse)){
                $warehouse[$row->warehouse_id] = SysPlatAccount::getAccountById($row->warehouse_id);
            }
            $temp['warehouse_id'] = $warehouse[$row->warehouse_id];
            $temp['sku'] = $row->sku->psku_code ?? '';
            $temp['b_num'] = $row->b_num;
            $temp['a_num'] = $row->a_num;
            $temp['num_per_box'] = $row->num_per_box;
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            if(!array_key_exists($row->auser_id,$user)){
                $user[$row->auser_id] = User::getUsernameById($row->auser_id);
            }
            $temp['auser_id'] = $user[$row->auser_id];
            $temp['created_at'] = $row->created_at;
            yield $temp;
        }
    }
}
